/*
 * TripEditPanel.java
 *
 * Created on March 30, 2006, 8:07 PM
 */

package sheep.view;


import java.awt.Component;


import java.awt.Point;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import org.jdesktop.animation.timing.Animator;
import org.jdesktop.animation.timing.TimingTarget;
import org.jdesktop.animation.timing.interpolation.PropertySetter;

import org.jdesktop.swingx.JXPanel;


import java.awt.BorderLayout;
import sheep.controller.Workspace;
import sheep.model.Task;
import sheep.view.utils.swingx.PainterBlackCustom;

/**
 *
 * @author  jm158417
 */
public class VisualGraphEditPanel extends javax.swing.JPanel {

  private Workspace workSpace;


    public VisualGraphEditPanel(Workspace workSpace) {
      this.workSpace = workSpace;
        initComponents();
        grapheViewer.addPropertyChangeListener("zoom",new PropertyChangeListener() {
      @Override
            public void propertyChange(PropertyChangeEvent evt) {
                zoomSlider.setValue(15);
            }
        });
        
       
        
        grapheViewer.setZoom(6);
        


        addFadeAdapter(jXPanel1);
        addFadeAdapter(jXPanel2);
        addFadeAdapter(jXPanel3);


        grapheViewer.setRecenterOnClickEnabled(false);


        this.setTransferHandler(null);

    }

    private static void addFadeAdapter(JXPanel panel) {
        MouseAdapter adapter = new FadeAdapter(panel);
        addFadeAdapter(adapter, panel);
    }

    private static void addFadeAdapter(MouseAdapter a, JComponent c) {
        c.addMouseListener(a);
        for (Component child : c.getComponents()) {
            if (child instanceof JComponent) {
                addFadeAdapter(a, (JComponent) child);
            }
        }
    }

    private static class FadeAdapter extends MouseAdapter {
        private Animator enterController;
        private Animator exitController;
        private JXPanel panel;
        private float originalOpacity;

        private FadeAdapter(JXPanel panel) {
            this.panel = panel;
            this.originalOpacity = panel.getAlpha();
        }

        @Override
        public void mouseEntered(MouseEvent evt) {
            if (evt.getButton() != 0) {
                return;
            }

            if (enterController != null && enterController.isRunning()) {
                return;
            }

            if (exitController != null && exitController.isRunning()) {
                exitController.stop();
            }

            enterController = PropertySetter.createAnimator(400, panel,"alpha",panel.getAlpha(), 0.999999f);
            enterController.addTarget(new TimingTarget() {
        @Override
                public void begin() {}
        @Override
                public void end() {}
        @Override
                public void timingEvent(float f) {panel.repaint();}
        @Override
                public void repeat() {}
            });
            enterController.setAcceleration(0.7f);
            enterController.setDeceleration(0.3f);
            enterController.start();
        }

        @Override
        public void mouseExited(MouseEvent evt) {
            Point p = (Point) evt.getLocationOnScreen().clone();
            SwingUtilities.convertPointFromScreen(p, panel.getParent());
            if (panel.getBounds().contains(p)) 
                return;
            if (exitController != null && exitController.isRunning()) 
                return;
            if (enterController != null && enterController.isRunning())
                enterController.stop();
            exitController = PropertySetter.createAnimator(400, panel, "alpha", panel.getAlpha(), originalOpacity);
            exitController.addTarget(new TimingTarget() {
        @Override
                public void begin() {}
        @Override
                public void end() {}
        @Override
                public void timingEvent(float f) {panel.repaint();}
        @Override
                public void repeat() {}
            });
            exitController.setAcceleration(0.7f);
            exitController.setDeceleration(0.3f);
            exitController.start();
        }
    }

    public GrapheViewer getGraphPanel() {
        return grapheViewer;
    }





    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        PainterBlackCustom aerithPanelPainter1 = new PainterBlackCustom();
        buttonGroup2 = new javax.swing.ButtonGroup();
        grapheViewer = new GrapheViewer(this.workSpace);
        jXPanel2 = new org.jdesktop.swingx.JXPanel();
        jButton1 = new javax.swing.JButton();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        zoomSlider = new javax.swing.JSlider();
        zoomOut = new javax.swing.JButton();
        zoomIn = new javax.swing.JButton();
        editContainer = new javax.swing.JPanel();
        jXPanel3 = new org.jdesktop.swingx.JXPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();

        JXPanel contentPanel = new JXPanel();
        
        setMinimumSize(new java.awt.Dimension(855, 85));
        //setOpaque(false);
        setPreferredSize(new java.awt.Dimension(855, 97));
        setLayout(new java.awt.BorderLayout());

        contentPanel.setOpaque(false);
        contentPanel.setLayout(new java.awt.GridBagLayout());
        
        grapheViewer.setOpaque(false);
        grapheViewer.setZoom(13);
        //grapheViewer.setLayout(new java.awt.GridBagLayout());

        jXPanel2.setAlpha(0.6F);
        jXPanel2.setAutoscrolls(true);
        jXPanel2.setBackgroundPainter(aerithPanelPainter1);
        jXPanel2.setMinimumSize(new java.awt.Dimension(65, 38));
        jXPanel2.setPreferredSize(new java.awt.Dimension(100, 38));
        jXPanel2.setLayout(new java.awt.GridLayout(1, 0));

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jButton1.setForeground(new java.awt.Color(255, 204, 51));
        jButton1.setText("Aide");
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.setFocusPainted(false);
        jXPanel2.add(jButton1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        contentPanel.add(jXPanel2, gridBagConstraints);

        jXPanel1.setAlpha(0.6F);
        jXPanel1.setBackgroundPainter(aerithPanelPainter1);
        jXPanel1.setMaximumSize(new java.awt.Dimension(300, 38));
        jXPanel1.setMinimumSize(new java.awt.Dimension(230, 38));
        jXPanel1.setPreferredSize(new java.awt.Dimension(300, 38));
        jXPanel1.setLayout(new java.awt.BorderLayout());

        zoomSlider.setBackground(new java.awt.Color(204, 204, 204));
        zoomSlider.setForeground(new java.awt.Color(255, 204, 102));
        zoomSlider.setMajorTickSpacing(5);
        zoomSlider.setMaximum(15);
        zoomSlider.setMinorTickSpacing(1);
        zoomSlider.setSnapToTicks(true);
        zoomSlider.setValue(2);
        zoomSlider.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        zoomSlider.setFocusable(false);
        zoomSlider.setMaximumSize(new java.awt.Dimension(260, 27));
        zoomSlider.setMinimumSize(new java.awt.Dimension(260, 27));
        zoomSlider.setPreferredSize(new java.awt.Dimension(260, 27));
        zoomSlider.addChangeListener(new javax.swing.event.ChangeListener() {
      @Override
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                zoomSliderStateChanged(evt);
            }
        });
        jXPanel1.add(zoomSlider, java.awt.BorderLayout.CENTER);

        zoomOut.setBackground(new java.awt.Color(255, 204, 51));
        zoomOut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/zoom-out.png"))); // NOI18N
        zoomOut.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 1, 2, 5));
        zoomOut.setBorderPainted(false);
        zoomOut.setContentAreaFilled(false);
        zoomOut.setFocusPainted(false);
        zoomOut.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        zoomOut.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        zoomOut.setIconTextGap(0);
        zoomOut.setMargin(new java.awt.Insets(0, 0, 0, 0));
        zoomOut.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/zoom-out-pressed.png"))); // NOI18N
        zoomOut.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/zoom-out-over.png"))); // NOI18N
        zoomOut.addActionListener(new java.awt.event.ActionListener() {
      @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zoomOutActionPerformed(evt);
            }
        });
        jXPanel1.add(zoomOut, java.awt.BorderLayout.LINE_END);

        zoomIn.setBackground(new java.awt.Color(255, 204, 51));
        zoomIn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/zoom-in.png"))); // NOI18N
        zoomIn.setBorder(javax.swing.BorderFactory.createEmptyBorder(2, 5, 2, 1));
        zoomIn.setBorderPainted(false);
        zoomIn.setContentAreaFilled(false);
        zoomIn.setFocusPainted(false);
        zoomIn.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        zoomIn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        zoomIn.setIconTextGap(0);
        zoomIn.setMargin(new java.awt.Insets(0, 0, 0, 0));
        zoomIn.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/zoom-in-pressed.png"))); // NOI18N
        zoomIn.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/zoom-in-over.png"))); // NOI18N
        zoomIn.addActionListener(new java.awt.event.ActionListener() {
      @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                zoomInActionPerformed(evt);
            }
        });
        jXPanel1.add(zoomIn, java.awt.BorderLayout.LINE_START);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        contentPanel.add(jXPanel1, gridBagConstraints);

        editContainer.setOpaque(false);
        editContainer.setLayout(null);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        contentPanel.add(editContainer, gridBagConstraints);

        jXPanel3.setAlpha(0.6F);
        jXPanel3.setBackgroundPainter(aerithPanelPainter1);
        jXPanel3.setMinimumSize(new java.awt.Dimension(300, 25));
        jXPanel3.setPreferredSize(new java.awt.Dimension(300, 38));
        jXPanel3.setLayout(new java.awt.GridLayout(1, 0));

        buttonGroup2.add(jCheckBox1);
        jCheckBox1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jCheckBox1.setForeground(new java.awt.Color(255, 204, 51));
        jCheckBox1.setText("Mouvement");
        jCheckBox1.setContentAreaFilled(false);
        jCheckBox1.setFocusPainted(false);
        jCheckBox1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jXPanel3.add(jCheckBox1);

        buttonGroup2.add(jCheckBox2);
        jCheckBox2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jCheckBox2.setForeground(new java.awt.Color(255, 204, 51));
        jCheckBox2.setText("Edition");
        jCheckBox2.setContentAreaFilled(false);
        jCheckBox2.setFocusPainted(false);
        jCheckBox2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jCheckBox2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox2ActionPerformed(evt);
            }
        });
        jXPanel3.add(jCheckBox2);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        contentPanel.add(jXPanel3, gridBagConstraints);

        grapheViewer.add(contentPanel, BorderLayout.CENTER);
        add(grapheViewer, BorderLayout.CENTER);
        refreshGraph();
    }                    

    @SuppressWarnings("unchecked")                
    private void zoomInActionPerformed(java.awt.event.ActionEvent evt) {                                       

    }                                      

    private void zoomSliderStateChanged(javax.swing.event.ChangeEvent evt) {                                        
        grapheViewer.setZoom(15 - zoomSlider.getValue());
    }                                       

    private void zoomOutActionPerformed(java.awt.event.ActionEvent evt) {                                        
    }                                       

    private void jCheckBox2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
    }                                          
    

   public void refreshGraph(){
     if (this.workSpace.getCurrentTask()!=null){
        grapheViewer.refreshGraph();
        repaint();
     }
   }
       
    // Variables declaration - do not modify                     
    private javax.swing.ButtonGroup buttonGroup2;
    javax.swing.JPanel editContainer;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    private org.jdesktop.swingx.JXPanel jXPanel2;
    private org.jdesktop.swingx.JXPanel jXPanel3;
    private GrapheViewer grapheViewer;
    private javax.swing.JButton zoomIn;
    private javax.swing.JButton zoomOut;
    private javax.swing.JSlider zoomSlider;
    // End of variables declaration                   
    private JXPanel contentPanel;
   
  
    

}
